package com.ruitu.mall_98_2.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyRefreshBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.TimeUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.CheckBillContract;
import com.ruitu.mall_98_2.mvp.model.CheckBillModel;
import com.ruitu.mall_98_2.mvp.presenter.CheckBillPresenter;
import com.ruitu.mall_98_2.order.OrderDetailsActivity;
import com.ruitu.mall_98_2.order.fragment.AfterSalesDetailActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.mine.Bills;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.util.ArrayList;
import java.util.List;

public class CheckBillActivity extends MyRefreshBaseActivity<CheckBillPresenter, CheckBillModel> implements CheckBillContract.View {

    private List<Integer> skipId = new ArrayList<>();
    private List<Bills> bills;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reReq();
        setHeadTitle("账单");
        skipId.add(R.id.header);
        setLoadMoreEnabled(false);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_check_bill;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    protected void reqData() {

    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lRecyclerView);
    }

    @Override
    protected void reqFirst() {
        reReq();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {
        showProgress();
        mPresenter.bills(AppHolder.instance().getUser().getAccountId());
//        mPresenter.bills("5085417586742608125");
    }

    @Override
    public void setData(List<Bills> bills) {
        this.bills = bills;
        refreshComplete();
        if (isDataEmpty(bills)) {
            showEmptyView(skipId);
        } else {
            showContent(null);
            adapter.setDataList(bills);
        }
    }

    @Override
    public void setFail() {
        refreshComplete();
        showError(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reReq();
            }
        });
    }

    DataAdapter adapter;

    class DataAdapter extends ListBaseAdapter<Bills> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_bills;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            ImageView iv_order_type = holder.getView(R.id.iv_order_type);
            TextView tv_order_status = holder.getView(R.id.tv_order_status);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_order_money = holder.getView(R.id.tv_order_money);
            LinearLayout ll_item = holder.getView(R.id.ll_item);
            Bills bills = mDataList.get(position);

            tv_order_status.setText(bills.getTitle());
            tv_time.setText(TimeUtils.millis2String(bills.getCreateTime()));

            if (TextUtils.equals(bills.getInfoType(), "0")) {
                Glide.with(mContext).load(R.mipmap.ic_return_money).into(iv_order_type);
            } else if (TextUtils.equals(bills.getInfoType(), "1")) {
                Glide.with(mContext).load(R.mipmap.ic_whitdraw_deposit).into(iv_order_type);
            } else {
                Glide.with(mContext).load(R.mipmap.buy).into(iv_order_type);
            }

            if (TextUtils.equals(bills.getInfoStatus(), "0")) {
                tv_order_money.setText("+" + bills.getMoney() + "元");
                tv_order_money.setTextColor(getResources().getColor(R.color.red));
            } else {
                tv_order_money.setText("-" + bills.getMoney() + "元");

            }

            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bills bill = mDataList.get(position);
                    if (bill.getInfoType().equals("0")) {
                        Intent intent = new Intent();
                        intent.putExtra("id", bill.getInfoId());
                        intent.setClass(CheckBillActivity.this, AfterSalesDetailActivity.class);
                        startActivity(intent);
                    } else if (bill.getInfoType().equals("3")) {
                        SmallOrder smallOrder = new SmallOrder();
                        smallOrder.setId(bill.getInfoId());
                        Intent intent1 = new Intent();
                        intent1.putExtra("smallOrder", smallOrder);
                        intent1.setClass(CheckBillActivity.this, OrderDetailsActivity.class);
                        startActivity(intent1);

                }
            }
        });
    }
}

}
